// pages/OrderReturn/OrderReturn.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    language: '',
    refundMoney: '',
    address: [],
    checked: {},
    options: [],
    express: '',
    expressCode: '',
    expressSN: '',
    popupVisible: false,
    // 订单id
    orderId: '',
    // 租赁订单id
    id: '5',
    // 订单状态
    order_status: 0,
    // 审核状态
    check_status: '',
    checkedindex: '',
    type:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.setData({
        id: options.id
      });
    }
    if( options.type == 'yz_lease_apply_adopt' ){
      this.setData({
        order_status:2,
        type : options.type
      })
      this.getYz_lease_Adress();
      this.getExpress();
    }else{
      this.applyData();
    }
  // this.getAddress()
  // this.getExpress()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en});
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  // 申请退还
  applyData() {
    var that = this;
    let urlStr = app.getNetAddresss('plugin.lease-toy.api.retreat.return.index');
    urlStr += '&order_id=' + this.data.id;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          let objData = res.data.lease_order;
          that.setData({
            refundMoney: objData.deposit_total,
            orderId: objData.order_id,
            id: objData.id,
            order_status: objData.return_status,
            check_status: objData.return_name
          });
          if (app._isTextEmpty(res.data.return_address)) {
            wx.showToast({
              title: '请填写退还地址',
              icon: 'none',
              duration: 1500,
              success: function () {
                setTimeout(function () {
                  // 要延时执行的代码
                  wx.navigateBack({
                    delta: 1
                  });
                }, 1500); // 延迟时间
              }
            });
          } else {
            that.setData({
              address: res.data.return_address
            });
            that.data.address.forEach(item => {
              if (item.is_default == 1) {
                that.setData({
                  checked: item
                });
              }
            });
          }
          that.setData({
            options: res.data.express_companies
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getAddress() {
    let urlStr = app.getNetAddresss('plugin.lease-toy.api.return-address.index');
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res.msg);
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  // 获取快递
  getExpress() {
    var that = this;
    let urlStr = app.getNetAddresss('plugin.yz-supply-lease.api.return.express');
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            options: res.data
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getYz_lease_Adress(){
    let urlStr = app.getNetAddresss('plugin.yz-supply-lease.api.return.return-address');
    app._getNetWork({
      url: urlStr,
      data:{
        order_id:this.data.id
      },
      success: (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            address: res.data.return_address
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  chooseAddress(e) {
    let val = e.detail;
    console.log(val)
    this.setData({
      checkedindex: val,
      checked: this.data.address[val]
    });
  },
  expressSNinp(e) {
    let val = e.detail.value;
    console.log(val);
    this.setData({
      expressSN: val
    });
  },
  expressShow() {
    this.setData({
      popupVisible: true
    });
  },
  expressClose() {
    this.setData({
      popupVisible: false
    });
  },
  expressChoose(e) {
    let val = e.currentTarget.dataset.item;
    this.initExpress();
    this.setData({
      express: val.name,
      expressCode: val.value,
      popupVisible: false
    });
  },
  initExpress() {
    this.setData({
      express: '',
      expressCode: ''
    });
  },
  // 提交审核
  submit() {
    console.log(this.data.checked)
    if (app._isTextEmpty(this.data.checked.address)) {
      wx.showToast({
        icon: 'none',
        title: '请选择归还地址',
        duration: 1500
      });
      return;
    }
    if (app._isTextEmpty(this.data.express)) {
      wx.showToast({
        icon: 'none',
        title: '请选择退还快递',
        duration: 1500
      });
      return;
    }
    if (app._isTextEmpty(this.data.expressCode)) {
      wx.showToast({
        icon: 'none',
        title: '快递code不能为空',
        duration: 1500
      });
      return;
    }
    if (app._isTextEmpty(this.data.expressSN)) {
      wx.showToast({
        icon: 'none',
        title: '快递编号不能为空',
        duration: 1500
      });
      return;
    }
    
    let urlStr = '';
    if( this.data.type == 'yz_lease_apply_adopt' ){
      urlStr = app.getNetAddresss('plugin.yz-supply-lease.api.return.return-form');
      urlStr += '&order_id=' + this.data.id;
      urlStr += '&express_no=' + this.data.expressSN;
      urlStr += '&company_name=' + this.data.express;
      urlStr += '&company_code=' + this.data.expressCode;
      urlStr += '&address_id=' + this.data.checked.id;
      urlStr += '&address=' + this.data.checked.address;
      urlStr += '&username=' + this.data.checked.contact_name;
      urlStr += '&tel=' + this.data.checked.mobile;
    }else{
      urlStr = app.getNetAddresss('plugin.lease-toy.api.retreat.return.submit-return');
      urlStr += '&order_id=' + this.data.orderId;
      urlStr += '&lease_id=' + this.data.id;
      urlStr += '&express_code=' + this.data.expressCode;
      urlStr += '&express_company_name=' + this.data.express;
      urlStr += '&express_sn=' + this.data.expressSN;
      urlStr += '&address=' + this.data.checked.address;
      urlStr += '&mobile=' + this.data.checked.mobile;
      urlStr += '&realname=' + this.data.checked.contact_name;
    }
    app._getNetWork({
      url: urlStr,
      success: (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            title: '提交成功',
            icon: 'none',
            duration: 1500,
            success: function () {
              setTimeout(function () {
                // 要延时执行的代码
                wx.navigateBack({
                  delta: 1
                });
              }, 1500); // 延迟时间
            }
          });
        } else {
          wx.showToast({
            title: '提交失败',
            icon: 'none',
            duration: 1500,
            success: function () {
              setTimeout(function () {
                // 要延时执行的代码
                wx.navigateBack({
                  delta: 1
                });
              }, 1500); // 延迟时间
            }
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  }
});
